//
//  HEAfterTextViewCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/13.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEAfterTextViewCell.h"

@interface HEAfterTextViewCell ()<QMUITextViewDelegate>

@property(nonatomic, strong) QMUITextView *textView;

@property (nonatomic, strong) UILabel *titleLb;

@end

@implementation HEAfterTextViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    // 商品名称
    _titleLb = [UILabel new];
    _titleLb.text = @"问题描述";
    _titleLb.font = DYFont(16);
    [self.contentView addSubview:_titleLb];
    
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(15);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);

    }];
    
    _textView = [QMUITextView new];
    // 限制可输入的字符长度
    self.textView.maximumTextLength = 1500;
    self.textView.placeholder = @"请详细描述您遇到的问题~";
    self.textView.placeholderColor = UIColorPlaceholder; // 自定义 placeholder 的颜色
    self.textView.textContainerInset = UIEdgeInsetsMake(5, 7, 5, 7);
    self.textView.typingAttributes = @{NSFontAttributeName: UIFontMake(16),
                                       NSParagraphStyleAttributeName: [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:20]
    };
    _textView.backgroundColor = CSplitLineColor;
    [self.contentView addSubview:_textView];
    self.textView.layer.cornerRadius = 4;
    [_textView setDelegate:self];
    
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLb.mas_bottom).mas_offset(10);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
        make.height.mas_greaterThanOrEqualTo(180);
        make.bottom.equalTo(self.contentView.mas_bottom).mas_offset(-15);
    }];
}

#pragma mark - <QMUITextViewDelegate>
// 实现这个 delegate 方法就可以实现自增高
- (void)textView:(QMUITextView *)textView newHeightAfterTextChanged:(CGFloat)height {
    
}

- (void)textView:(QMUITextView *)textView didPreventTextChangeInRange:(NSRange)range replacementText:(NSString *)replacementText {
    [Dialog toast:[NSString stringWithFormat:@"文字不能超过 %@ 个字符", @(textView.maximumTextLength)] delay:2.0];
}

- (void)refreshContent:(GDFiterCellModel *)fiterModel formModel:(GDFormModel *)formModel indexPath:(NSIndexPath *)indexPath {
    _textView.text = formModel.as_content;
}

- (void)textViewDidChange:(UITextView *)textView {
    self.formModel.as_content = textView.text;
}

@end
